﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class event_wxdemo_index : System.Web.UI.Page
{
    /// <summary>
    /// 微信公众号id
    /// </summary>
    public string m_configkey = "JuZhenHuDongKeJi";
    protected void Page_Load(object sender, EventArgs e)
    {
        GetPingTaiWeiXinData();
        if (Session["weixin_bkurl"] != null)
        {
            string url = Session["weixin_bkurl"].ToString();
            Session["weixin_bkurl"] = null;
            Response.Redirect(UnEscape(url));
        }
        else
        {
            Response.Redirect("index.html");
        }
    }


    #region 以下为固定代码
    public static string UnEscape(string str)
    {
        StringBuilder sb = new StringBuilder();
        int len = str.Length;
        int i = 0;
        while (i != len)
        {
            if (Uri.IsHexEncoding(str, i))
                sb.Append(Uri.HexUnescape(str, ref i));
            else
                sb.Append(str[i++]);
        }
        return sb.ToString();
    }


    /// <summary>
    /// 授权界面获取授权
    /// </summary>
    public void GetPingTaiWeiXinData()
    {

        //如果获取到平台返回的值
        if (docy.str(Request["re"]) != "")
        {
            string re = DoPingTaiRe(Request["re"]);
            if (re != "OK")
            {
                Response.Write(re);
                Response.End();
                return;
            }
        }
        else
        {
            //向平台申请数据
            string url = "http://wx.juzhen.net";
            //保存返回地址
            string bkurl = docy.str(Request["bkurl"]);
            if (bkurl != "")
            {
                Session["weixin_bkurl"] = bkurl;
            }
            if (Session["WeiXinData"] == null)
            {
                string _dangQianUrl = Request.Url.Scheme + "://" + Request.Url.Host + Request.Path;
                string urlcs = "/weixin_ShouQuanV2/main.aspx?type=getOpenid-bj-" + m_configkey + "-bj-" + _dangQianUrl; 
                Response.Redirect(url + urlcs);
            }
        }
 
    }

    public string DoPingTaiRe(string re)
    {
        string[] _relist;
        DateTime _time;
        string nickName = "";
        string imgUrl = "";
        string openid = "";

        try
        {
            _relist = docy.Split(docy.jiemicy(re), "-bj-");
        }
        catch
        {
            return "授权参数错误";
        }
        try
        {
            _time = DateTime.Parse(_relist[1]);
        }
        catch
        {
            return "时间格式错误";
        }

        if ((DateTime.Now - _time).TotalSeconds > 10)
        {
            return "链接超时，请重新访问！";
        }
        try
        {
            nickName = _relist[0];
            imgUrl = _relist[2];
            openid = _relist[3];
        }
        catch
        {
            return "授权参数错误";
        }
        WeiXinData reOb = new WeiXinData();
        reOb.openid = openid;
        reOb.nickName = nickName;
        reOb.imgUrl = imgUrl;
        Session["PingTaiWeiXinData"] = reOb;
        return "OK";
    }
    #endregion




}